(function (vc) {

    vc.extends({
        data: {
            registerInfo: {
                username: '',
                password: '',
                repassword: '',
                errorInfo: '',
                validateInfo: '',
                tel: '',
                messageCode: '',
                logo: '',
                levelCd:'03',
                buttonInfo: "验证码"
            }
        },
        _initMethod: function () {

        },
        _initEvent: function () {
        },
        methods: {
            showTip1:function(){
                $(".pa2").show();
                $("#mask").css("height", $(window).outerHeight()).show();
            },
            showTip2:function(){
                $(".pa3").show();
                $("#mask").css("height", $(window).outerHeight()).show();
            },
            closeTips:function(){
                $("#mask").hide();
                $(".pa2").hide();
                $(".pa3").hide();
            },
            changeUType:function(_tempTab){
                vc.component.registerInfo.levelCd= _tempTab;
            },
            validate:function(){
                return vc.validate.validate({
                    registerInfo:vc.component.registerInfo
                },{
                    'registerInfo.username':[
                        {
                            limit:"required",
                            param:"",
                            errInfo:"用户名不能为空"
                        },
                        {
                            limit:"maxin",
                            param:"4,12",
                            errInfo:"用户名长度必须在4位至6位"
                        },
                    ],
                    'registerInfo.password':[
                        {
                            limit:"required",
                            param:"",
                            errInfo:"密码不能为空"
                        },
                        {
                            limit:"maxin",
                            param:"6,12",
                            errInfo:"密码长度必须在6位至12位"
                        },
                    ],
                    'registerInfo.tel':[
                        {
                            limit:"required",
                            param:"",
                            errInfo:"手机号不能为空"
                        },
                        {
                            limit:"phone",
                            param:"",
                            errInfo:"不是有效的手机号"
                        }
                    ],
                    'registerInfo.messageCode':[
                        {
                            limit:"required",
                            param:"",
                            errInfo:"验证码不能为空"
                        },
                        {
                            limit:"num",
                            param:"",
                            errInfo:"验证码必须是数字"
                        }
                    ],

                });
            },
            doRegister() {
                if(!vc.component.validate()){
                    vc.toast(vc.validate.errInfo);
                    return ;
                }
                if(!$("#agree").prop("checked")){
                    vc.toast("请先勾选平台协议");
                    return ;
                }
                vc.http.apiPost('/register.pcUserRegister',
                            JSON.stringify(vc.component.registerInfo),
                            {
                                emulateJSON:true
                             },
                             function(json,res){
                                var _json = JSON.parse(json);
                                if(res.status == 200 && _json.code == '0'){
                                    if(vc.component.registerInfo.levelCd =='03'){
                                        vc.jumpToPage('/pageLR.html#/pages/plateform/login');
                                    }else{
                                        vc.jumpToPage("/user.html#/pages/frame/login");
                                    }
                                    return ;
                                    vc.toast("注册成功");
                                }else{
                                    vc.toast(_json.msg);
                                }
                             },
                             function(errInfo,error){
                                console.log('请求失败处理');
                                vc.toast(errInfo);
                                vc.component.registerInfo.errorInfo = errInfo;
                             });
            },
            sendTelMessageCode() {
                if (!vc.validate.phone(vc.component.registerInfo.tel)) {
                    vc.toast("手机号码为空或不正确，不能发送验证码");
                    return;
                }

                if (vc.component.registerInfo.buttonInfo != '验证码') {
                    return;
                }
                // 开启定时
                vc.component.messageCodeTimer();
                var param = {
                    params: {
                        tel: vc.component.registerInfo.tel
                    }
                };
                vc.http.apiGet('/register.messageCode',
                    param,
                    function (json, res) {
                        if (res.status == 200) {
                            vc.component.registerInfo.validateInfo = json;
                            return;
                        }
                    }, function (errInfo, error) {
                        vc.toast("手机验证码获取异常");

                    });
            },
            //验证码定时
            messageCodeTimer() {
                var num = 60;
                var _timer = vc.createTimer(function () {
                    num--;
                    vc.component.registerInfo.buttonInfo = num + " 秒后重试";
                    if (num === 1) {
                        vc.clearTimer(_timer);
                        vc.component.registerInfo.buttonInfo = "验证码";
                    }
                }, 1000);
            },
            _doLogin(){
                vc.jumpToPage('/pageLR.html#/pages/plateform/login');
            }

        }
    });

})(window.vc);
